'use strict';
const koa = require('koa');
const route = require('koa-route');
const app = koa();

//模板
var views = require('co-views');
var render = views('./view', {
    map: { html: 'ejs'}
})
var service = require('./service/webAppService.js');

//静态文件
const koa_static = require('koa-static-server');
app.use(koa_static({
    rootDir: './static/',
    rootPath: '/static/',
    maxage: 0,
}))

app.use(route.get('/hello', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = 'Hello koa!';
}));

// 模板
app.use(route.get('/ejs_test', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = yield render('test', {title: 'title_test'});
}));

app.use(route.get('/', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = yield render('index', {title: '书城首页'});
}));

app.use(route.get('/male', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = yield render('male', {title: '男生频道'});
}));

app.use(route.get('/female', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = yield render('female', {title: '女生频道'});
}));

app.use(route.get('/search', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = yield render('search', {title: '搜索'});
}));

app.use(route.get('/rank', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = yield render('rank', {title: '排行'});
}));

app.use(route.get('/category', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = yield render('category', {title: '分类'});
}));

var qs = require('querystring');
app.use(route.get('/book', function*(){
    this.set('Cache-Control', 'no-cache');
    var params = qs.parse(this.req._parsedUrl.query);
    var bookId = params.id;
    this.body = yield render('book', {bookId: bookId});
}));

// mock
app.use(route.get('/api_test', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = service.get_test_data();
}));

app.use(route.get('/ajax/index', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = service.get_index_data();
}));

app.use(route.get('/ajax/female', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = service.get_female_data();
}));

app.use(route.get('/ajax/male', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = service.get_male_data();
}));

app.use(route.get('/ajax/rank', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = service.get_rank_data();
}));

app.use(route.get('/ajax/category', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = service.get_category_data();
}));

app.use(route.get('/ajax/bookbacket', function*(){
    this.set('Cache-Control', 'no-cache');
    this.body = service.get_bookbacket_data();
}));


var qs = require('querystring');
app.use(route.get('/ajax/book',function*(){
    this.set('Cache-Control', 'no-cache');
    var params = qs.parse(this.req._parsedUrl.query);
    var id = params.id;
    if(!id) {
        id = "";
    }
    this.body = service.get_book_data(id);
}));

// 请求转发和接口转发
app.use(route.get('/ajax/search', function*(){
    this.set('Cache-Control', 'no-cache');
    var params = qs.parse(this.req._parsedUrl.query);
    var start = params.start;
    var end = params.end;
    var keyword = params.keyword;
    this.body = yield service.get_search_data(start, end, keyword);
}));


app.listen(3000);

console.log("koa server is started!");